package com.suixun.controller;

import java.util.HashMap;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.util.concurrent.FailureCallback;
import org.springframework.util.concurrent.ListenableFuture;
import org.springframework.util.concurrent.SuccessCallback;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.authority.controller.BaseManager;
import com.google.gson.Gson;
import com.suixun.controller.vo.SendMessForm;
import com.suixun.entity.form.SendMessageForm;
import com.suixun.service.SendMessageService;
import com.suixun.util.Constant;
import com.suixun.vo.SendMessageVO;
/**
 * 
 * <p>Title: SendMessageController</p>
 * <p> 功能描述:消息推送 </p> 
 * @author	陈显强
 * @date	2016年12月21日上午11:12:31
 * @version 1.0
 */
@Controller
@RequestMapping("/sendMessageManage")
public class SendMessageController extends BaseManager {
	@Resource
	private SendMessageService sendMessageService;
	
	protected final static Logger logger = LoggerFactory.getLogger(SendMessageController.class); 
	
	/**
	 * 
	 * <p>Title: querySendMessageList</p>
	 * <p> 功能描述:查询消息推送列表 </p>
	 * @param param
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/querySendMessageList.json")
	public Object querySendMessageList(String param){
		boolean bln=checkRight();
		if(!bln) {
			return fail(Constant.Authority.CHECK_FALSE);
		}
		SendMessageForm sendMessage=new Gson().fromJson(param, SendMessageForm.class);
		try {
			return success(sendMessageService.querySendMessageList(sendMessage));
		} catch (Exception e) {
			logger("SendMessageController querySendMessageList:"+e.getMessage(),e);
			return fail(Constant.EXCEPTION.OPERATE_FAIL);
		}
	}
	
	/**
	 * 
	 * <p>Title: addSendMessage</p>
	 * <p> 功能描述:添加消息推送 </p>
	 * @param param
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/addSendMessage.json")
	public Object addSendMessage(String param){
		boolean bln=checkRight();
		/*if(!bln) {
			return fail(Constant.Authority.CHECK_FALSE);
		}
		if(StringUtils.isEmpty(param)){
			return fail(Constant.EMPTY);
		}*/
		try{
			SendMessageForm sendMessage=new Gson().fromJson(param, SendMessageForm.class);
			sendMessage.setOperationername(getUsername());
			if(!StringUtils.isNotEmpty(sendMessage.getTitle())){
				return fail(Constant.EMPTY);
			}
			int isAdd=sendMessageService.addSendMessage(sendMessage);
			HashMap<String ,String> result=new HashMap<String ,String>();
			if(isAdd==1){
				result.put("resultstatus", "1");
				result.put("resultinfo", Constant.OPERATE_SUCCESS);
				return success(result);
			}else{
				String message=null;
				if(isAdd==-1){
					message=Constant.EMPTY;
				}else if(isAdd==-2){
					message= Constant.SendMessage.SENDTIME_IS_FASLE;
				}else{
					message=Constant.SendMessage.CONTENT_IS_FALSE;
				}
				return fail(message);
			}
		}catch (Exception e) {
			logger("SendMessageController addSendMessage:"+e.getMessage(),e);
			return fail(Constant.EXCEPTION.OPERATE_FAIL);
		}
	}
	
	/**
	 * 
	 * <p>Title: querySendMessage</p>
	 * <p> 功能描述:查询单个推送消息 </p>
	 * @param param
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/querySendMessage.json")
	public Object querySendMessage(String param){
		boolean bln=checkRight();
		if(!bln) {
			return fail(Constant.Authority.CHECK_FALSE);
		}
		if(param==null){
			return fail(Constant.EMPTY);
		}
		try {
			SendMessageForm sendMessage=new Gson().fromJson(param, SendMessageForm.class);
			if (!StringUtils.isNotEmpty(sendMessage.getMessageid())) {
				return fail(Constant.EMPTY);
			}
			SendMessageVO sendMessageVO=sendMessageService.querySendMessage(sendMessage);
			if(sendMessageVO==null){
				return fail(Constant.EMPTY);
			}else{
				return success(sendMessageVO);
			}
		}catch (Exception e) {
			logger("SendMessageController querySendMessage:"+e.getMessage(),e);
			return fail(Constant.EXCEPTION.OPERATE_FAIL);
		}
	}
	
	/**
	 * 
	 * <p>Title: deleteSendMessage</p>
	 * <p> 功能描述:删除指定推送消息 </p>
	 * @param param
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/deleteSendMessage.json")
	public Object deleteSendMessage(String param){
		boolean bln=checkRight();
		if(!bln) {
			return fail(Constant.Authority.CHECK_FALSE);
		}
		if(param==null){
			return fail(Constant.EMPTY);
		}
		try {
			SendMessageForm sendMessage=new Gson().fromJson(param, SendMessageForm.class);
			if (!StringUtils.isNotEmpty(sendMessage.getMessageid())) {
				return fail(Constant.EMPTY);
			}
			int isDelete=sendMessageService.deleteSendMessage(sendMessage);
			HashMap<String ,String> result=new HashMap<String ,String>();
			if(isDelete==1){
				result.put("resultstatus", "1");
				result.put("resultinfo", Constant.OPERATE_SUCCESS);
				return success(result);
			}else{
				return fail(Constant.OPERATE_FAIL);
			}
		}catch (Exception e) {
			logger("SendMessageController deleteSendMessage:"+e.getMessage(),e);
			return fail(Constant.EXCEPTION.OPERATE_FAIL);
		}
	}
	
	/**
	 * 实时推送短信和app
	 */
	@RequestMapping(value="/send_message.json",method = RequestMethod.POST,consumes=MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public Object sendMesage(@RequestBody SendMessForm form)
	{
		boolean bln=checkRight();
		if(!bln) {
			return fail(Constant.Authority.CHECK_FALSE);
		}
		form.setOperationername(getUsername());
		try {
			
			 ListenableFuture<Integer> listenableFuture =sendMessageService.sendMessage(form);
			
			
			SuccessCallback<Integer> successCallback = result1 -> SendMessageController.logger.info("消息推送成功, 返回值 : " + result1.toString());
			
			FailureCallback failureCallback = throwable -> SendMessageController.logger.error("消息推送失败 : " + throwable.getMessage()); 

			listenableFuture.addCallback(successCallback, failureCallback);  
			
			Integer isSended = listenableFuture.get(1000,TimeUnit.MILLISECONDS);
			
			HashMap<String ,String> result=new HashMap<String ,String>();
			if(isSended==1){
				result.put("resultstatus", "1");
				result.put("resultinfo", Constant.OPERATE_SUCCESS);
				return success(result);
			}else{
				String message=null;
				if(isSended==-1){
					message=Constant.EMPTY;
				}else if(isSended==-2){
					message= Constant.SendMessage.SENDTIME_IS_FASLE;
				}else{
					message=Constant.SendMessage.CONTENT_IS_FALSE;
				}
				return fail(message);
			}
		}catch (Exception e) {
			if(e instanceof TimeoutException)
			{
				HashMap<String ,String> res=new HashMap<String ,String>();
				res.put("resultstatus", "1");
				res.put("resultinfo", "后台持续完成推送");
				return success(res);
			}
			logger("SendMessageController sendMessage:"+e.getMessage(),e);
			return fail(Constant.EXCEPTION.OPERATE_FAIL);
		}
	}
}
